<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>新增票务</title>
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/themes/icon.css">
<script type="text/javascript"
	src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/jquery.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">
	var url;
	function formatNickName(val, row) {
		if(val!=null){
		return val.nickName;
		}else{
			return '';
		}
	}
	function formatPayType(val, row) {
		if (val == 1) {
			return "现金";
		} else if (val == 2) {
			return "支付宝";
		} else if (val == 3) {
			return "现金支付";
		}else{
			return "其他支付";
		}
	}
	function formatOrderStatus(val, row) {
		if (val == 0) {
			return "<span style=color:red;>未支付</span> ";
		} else if (val == 1) {
			return "已支付";
		}
	}
	function formatConsumeStatus(val, row) {
		if (val == 0) {
			return "<span style=color:red;>未出票</span> ";
		} else if (val == 1) {
			return "已出票";
		}
	}
	//通过订单出票 
	function openOrderDialog() {
		var selectedRows = $("#dg").datagrid("getSelections");
		if (selectedRows.length != 1) {
			$.messager.alert("系统提示", "请选择一个需要出票订单！");
			return;
		}else{
		var row = selectedRows[0];
		$.post("${pageContext.request.contextPath}/ticket/byOrderList/"+row.id+".do", function(result){
			$("#page").append(result);
			$("#outTicketPage").dialog("open").dialog("setTitle", "通过订单出票");
		});
		}
	}
	
	//打开现金出票弹窗 
	function openCashDialog(){
		$("#cashTicket").dialog("open").dialog("setTitle", "现金出票");
	}
	//提交现金出票
	function saveByCashOutTicket() {
		$("#cashFm").form("submit", {
			url : "${pageContext.request.contextPath}/ticket/saveByCash.do?&manaage.id=${currentUser.id }&ticketType.id="+$("#cashTicketType").find("option:selected").val(),
			onSubmit : function() {
				if($("#cashTicketType").find("option:selected").val()=='0'){
					$.messager.alert("系统提示", "<font style='color:red'>你还没有选择票类型!</font>");
					return false;
				}
				return $(this).form("validate");
			},
			success : function(result) {
			var result = eval('(' + result + ')');
				if (result.success) {
					$("#dg").datagrid("reload");
					$.post("${pageContext.request.contextPath}/ticket/byOrderList/"+result.data+".do", function(result){
						if(result!=null){
						$("#page").append(result);
						$("#outTicketPage").dialog("open").dialog("setTitle", "通过订单打印出票信息 ");
						closeCashDialog();
						}else{
							$.messager.alert("系统提示", "出票失败！");
							return;
						}
					});
				} else {
					$.messager.alert("系统提示", "出票失败！");
					return;
				}
			}
		}); 
	}
	
	//关闭对话框
	function closeDialog() {
		resetValue();
		$("#orderTicket").dialog("close");
	}
	//关闭打印页面
	function closeOutTicketPage() {
		$("#outTicketPage").dialog("close");
		$(".ticketBox").remove();
	}
	//关闭现金出票页面
	function closeCashDialog() {
		$("#cashTicket").dialog("close");
	}
	
	function resetValue() {
		$("#odCount").val("");
		$("#obCount").val("");
	}
</script>


</head>
<body style="margin: 1px">
	<table id="dg" title="景区票售票管理" class="easyui-datagrid" fitColumns="true"
		pagination="true" rownumbers="true"
		url="${pageContext.request.contextPath}/ticketOrder/list.do?status=1"
		fit="true" toolbar="#tb">
		<thead>
			<tr>
				<th field="cb" checkbox="true" align="center"></th>
				<th field="id" width="30" align="center">订单编号</th>
				<th field="status" width="30" align="center" formatter="formatOrderStatus">订单状态</th>
				<th field="totalPrice" width="30" align="center">总金额</th>
				<th field="payType" width="30" align="center" formatter="formatPayType">支付方式</th>
				<th field="type" width="30" align="center">订单类型</th>
				<th field="orderNumber" width="30" align="center">商户订单编号</th>
				<th field="createTime" width="30" align="center">订单时间</th>
				<th field="consumeStatus" width="30" align="center" formatter="formatConsumeStatus">出票状态</th>
				<!-- <th field="userInfo" width="20" align="center" formatter="formatNickName">昵称</th> -->
			</tr>
		</thead>
	</table>
	
	<div id="tb">
		<div>
			<a href="javascript:openOrderDialog()" class="easyui-linkbutton"
				iconCls="icon-add" plain="true">选择订单出票</a>
				 &nbsp; &nbsp; &nbsp;
			<a href="javascript:openCashDialog()" class="easyui-linkbutton"
				iconCls="icon-add" plain="true">现金出票</a> 
				&nbsp; &nbsp; &nbsp;
			<a href="javascript:openTeamDialog()" class="easyui-linkbutton"
				iconCls="icon-add" plain="true">团队出票</a>
		</div>
	</div>
	<!--现金出票  -->
	<div id="cashTicket" class="easyui-dialog"
		style="width: 500px; height: 380px; padding: 10px 20px" closed="true"
		buttons="#dlg-buttons-cash">
		<form id="cashFm" method="post">
			<table cellspacing="8px">
				<tr>
					<td>票类型：</td>
					<td>
					<select id="cashTicketType">
							<option value="0">请选择票类...</option>
							<c:forEach var="ticketType" items="${ticketTypeList }">
								<option value="${ ticketType.id}">${ ticketType.name}</option>
							</c:forEach>
					</select></td>
				</tr>
				<tr>
					<td>成人数量：</td>
					<td><input type="text" value="0" name="adultCount" class="easyui-numberbox" id="cashodCount" onkeyup="pricechange();"/></td>
				</tr>
				<tr>
					<td>儿童数量：</td>
					<td><input type="text" value="0" name="childCount" class="easyui-numberbox" id="cashobCount" onkeyup="pricechange();"/></td>
				</tr>

				<tr id="cashAdultPrice">
					<td >成人票单价：</td>
					<td><input type="hidden" name="adultPrice" id="cashodPrice"/>
					<span>
					</span>
					</td>
				</tr>
				<tr id="cashChildPrice">
					<td >儿童票单价：</td>
					<td><input type="hidden" name="childPrice" id="cashobPrice"/>
					<span >
					</span>
					</td>
				</tr>
				<tr id="totalPriceSpanCash">
					<td>订单总价：</td>
					<td><input type="hidden" name="totalPrice" />
					<span></span>
					</td>
				</tr>
			</table>
		</form>
	</div>
	
	<div id="dlg-buttons-cash">
		<a href="javascript:saveByCashOutTicket()" class="easyui-linkbutton"
			iconCls="icon-ok">出票</a> <a href="javascript:closeCashDialog()"
			class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
	</div>
	
	
	
	<!--团队出票  -->
	<div id="TeamTicket" class="easyui-dialog"
		style="width: 500px; height: 380px; padding: 10px 20px" closed="true"
		buttons="#dlg-buttons-team">
		<form id="fm3" method="post">
			<table cellspacing="8px">
				<tr>
					<td>总价：</td>
					<td><input type="text" name="totalPrice" id="totalPrice" class="totalPriceSpan" /></td>
				</tr>
			</table>
		</form>
	</div>
	<div id="dlg-buttons-team">
		<a href="javascript:saveByCashOutTicket()" class="easyui-linkbutton"
			iconCls="icon-ok">出票</a> <a href="javascript:closeDialog()"
			class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
	</div>
	
	<!--打印票  -->
	<div id="outTicketPage" class="easyui-dialog"
		style="width: 500px; height: 580px; padding: 20px 20px" closed="true"
		buttons="#dlg-buttons-oTp">
		<div id="page"></div>
 	</div>
	
		<div id="dlg-buttons-oTp">
		<a href="javascript:myPrint();" class="easyui-linkbutton"
			iconCls="icon-ok">打印</a> <a href="javascript:closeOutTicketPage()"
			class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
		</div>
	
	<script type="text/javascript">
		$(document).ready(function() {
			 $("#cashTicketType").change(function() {
			 	$.post("${pageContext.request.contextPath}/ticketType/query.do",
						  {
						    id:$("#cashTicketType").find("option:selected").val()
						  },
						  function(data,status){
							  if(data!=null){
									$("#cashAdultPrice").find("span").text(data.adultPrice+"元");
									$("#cashChildPrice").find("span").text(data.childPrice+"元");
									$("#cashAdultPrice").find("input").val(data.adultPrice);
									$("#cashChildPrice").find("input").val(data.childPrice);
							  }else{
						 $("#cashAdultPrice").text("该类票暂无价格,请联系管理员!");
						  $("#cashChildPrice").text("该类票暂无价格,请联系管理员!");   
							  }
						  },"json");
			}); 
		});
		
		//现金出票计算价格 
		function pricechange(){
			var num=$("#cashodCount").val();
			var num2=$("#cashobCount").val();
			var adultPriceCash=$("#cashAdultPrice").find("input").val();
			var childPriceCash=$("#cashChildPrice").find("input").val();
			var totleprice=(num*adultPriceCash)+(num2*childPriceCash);
			$("#totalPriceSpanCash").find("span").text(totleprice);
		}
		//掉打印 机
		function myPrint() {
			var newWindow = window.open("打印窗口", "_blank"); //打印窗口要换成页面的url
			var docStr = $("#page").html();
			//alert(docStr);
			newWindow.document.write(docStr);
			newWindow.print();
			newWindow.document.close();
			newWindow.close();
		}
	
	</script>

</body>
</html>